/**
 * SSL Certificate Generation 
 */
#ifndef SSLGEN_H
#define SSLGEN_H
#include <filesystem>
#include <QString>
#include <QProcess>
#include <QDebug>

int ssl_genetate(std::string crt,std::string key)
{
    //SSL证书生成
    QProcess sslProcess;
    sslProcess.start(QStringLiteral("openssl"),
                     {
                         QStringLiteral("req"),
                         QStringLiteral("-nodes"),
                         QStringLiteral("-new"),
                         QStringLiteral("-x509"),
                         QStringLiteral("-keyout"),
                         key.c_str(),
                         QStringLiteral("-out"),
                         crt.c_str(),
                         QStringLiteral("-days"),
                         QStringLiteral("1"),
                         QStringLiteral("-batch"),
                     });
    sslProcess.waitForFinished();

    if (!std::filesystem::exists(crt) || !std::filesystem::exists(key)) {
        qCritical() << "Could not generate a certificate and no certificate provided. A valid TLS certificate and key should be provided.";
        return -1;
    } else {
        qWarning() << "Temporary certificate generated; ready to accept connections.";
    }
    return 0;
}

#endif